﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 旋转角度检查（检查有旋转角度的点的旋转角度属性是否赋值）
    /// </summary>
    public class FAngleExceptionCheck : Check
    {
        public int Check(string errType, IFeatureLayer pFeatLyr, IGeometry pGeometry, string whereClause, string fcodename, string fanglename)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

            int idxFcode = pFeatCls.FindField(fcodename);

            if (idxFcode == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到分类代码[" + fcodename + "]字段，检查失败！");
                return -1;
            }

            int idxFangele = pFeatCls.FindField(fanglename);

            if (idxFangele == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到旋转角度[" + fanglename + "]字段，检查失败！");
                return -1;
            }

            if (pFeatCls.ShapeType != esriGeometryType.esriGeometryPoint)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层不是点状图层，检查失败！");
                return -1;
            }

            ISpatialFilter pSpatialFileter = new SpatialFilterClass();
            if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;
            pSpatialFileter.WhereClause = whereClause;
            pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

            showFakePro(true);
            LogHelper.WriteLog("正在计算需要检查的要素数目...");

            int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);

            LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
            showFakePro(false);

            if (max == 0)
            {
                LogHelper.WriteLog("没有满足检查要求的要素存在。");
                return -1;
            }

            setProcessBarMax(max);

            IFeatureCursor pFeatCur;
            pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

            IFeature pFeature;

            int count = 0;
            int errCount = 0;

            while ((pFeature = pFeatCur.NextFeature() )!= null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                if (pFeature.get_Value(idxFcode) == DBNull.Value || pFeature.get_Value(idxFcode) == null)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "分类代码为空");
                    errCount++;
                    continue;
                }

                string fcodevalue = pFeature.get_Value(idxFcode).ToString();

                if(fcodevalue.Substring (7,1)=="2" && 
                    (pFeature.get_Value (idxFangele )== DBNull.Value || pFeature.get_Value (idxFangele )==null))
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "旋转角度缺失");
                    errCount++;
                    continue;
                }
            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("旋转角度异常检查结束。共发现" + errCount + "处错误！\n");

            return errCount;
        }
    }
}
